package	com.sdry.web.controller.tdd.sale;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sdry.model.lz.Customer;
import com.sdry.model.tdd.sale.SaleExpenseAccount;
import com.sdry.model.tdd.sale.SaleMaterielExpenseAccount;
import com.sdry.service.tdd.OthersService;
import com.sdry.service.tdd.sale.SaleExpenseAccountService;
import com.sdry.service.tdd.sale.SaleMaterielExpenseAccountService;
import com.sdry.utils.ResponseUtil;
/**
 *
 *@ClassName: SaleMaterielExpenseAccountController
 *@Description: 费用科目+物料
 *@Author tdd
 *@Date 2019-09-25 11:30:20
 *@version 1.0
*/
@Controller
@RequestMapping("/saleMaterielExpenseAccount")
public class SaleMaterielExpenseAccountController{
	@Resource SaleMaterielExpenseAccountService saleMaterielExpenseAccountService;
	@Resource SaleExpenseAccountService saleExpenseAccountService;
	@Resource OthersService othersService;
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageSaleMaterielExpenseAccount (Model model) {
		//物料科目
		SaleExpenseAccount saleExpenseAccount = new SaleExpenseAccount();
		saleExpenseAccount.setEtype("0");
		saleExpenseAccount.setEisTrue("0");
		List<SaleExpenseAccount> saleExpenseAccounts = saleExpenseAccountService.queryAllByMution(saleExpenseAccount);
		model.addAttribute("saleExpenseAccounts",saleExpenseAccounts);
		//供应商
		Customer param = new Customer();
		param.setCustomer_type("0");
		List<Customer> customers = othersService.customerQueryAllByMution(param);
		model.addAttribute("customers",customers);
		return "/td/sale/saleMaterielExpenseAccount";
	}
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	 */
	@RequestMapping("/pageReport")
	public String pageSaleMaterielExpenseAccountReport (Model model) {
		//物料科目
		SaleExpenseAccount saleExpenseAccount = new SaleExpenseAccount();
		saleExpenseAccount.setEtype("0");
		List<SaleExpenseAccount> saleExpenseAccounts = saleExpenseAccountService.queryAllByMution(saleExpenseAccount);
		model.addAttribute("saleExpenseAccounts",saleExpenseAccounts);
		//供应商
		Customer param = new Customer();
		param.setCustomer_type("0");
		List<Customer> customers = othersService.customerQueryAllByMution(param);
		model.addAttribute("customers",customers);
		return "/td/sale/saleMaterielExpenseAccountReport";
	}
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public SaleMaterielExpenseAccount queryById(Long id) {
		SaleMaterielExpenseAccount param = saleMaterielExpenseAccountService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<SaleMaterielExpenseAccount> queryAllByMution(SaleMaterielExpenseAccount param) {
		List<SaleMaterielExpenseAccount> list = saleMaterielExpenseAccountService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(SaleMaterielExpenseAccount param,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<SaleMaterielExpenseAccount> list = saleMaterielExpenseAccountService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = saleMaterielExpenseAccountService.selectCountByMution(param);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		ResponseUtil.write(response, jsonObject);
	}
	/** 
	 * 报表——条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/listReport")
	public void listReport(int page,int pageSize,String ename,String whereSql,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<SaleMaterielExpenseAccount> list = saleMaterielExpenseAccountService.findPageByMutionReport(page,pageSize,ename, whereSql);
		/*
		 * 条件查询总条数
		 */
		Integer total = saleMaterielExpenseAccountService.selectCountByMutionReport(ename, whereSql);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		ResponseUtil.write(response, jsonObject);
	}
	/** 
	 * 报表详情——条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/listReportDetails")
	public void listReportDetails(int page,int pageSize,String ename,String whereSql,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<SaleMaterielExpenseAccount> list = saleMaterielExpenseAccountService.findPageByMutionReportDetails(page,pageSize,ename, whereSql);
		/*
		 * 条件查询总条数
		 */
		Integer total = saleMaterielExpenseAccountService.selectCountByMutionReportDetails(ename, whereSql);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		ResponseUtil.write(response, jsonObject);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(SaleMaterielExpenseAccount param, HttpServletResponse response) throws Exception{
		Long id = saleMaterielExpenseAccountService.insert(param);
		ResponseUtil.write(response,id);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/insertPL")
	public void insertPL(SaleMaterielExpenseAccount param,Long[] mids,String[] unitPrices, HttpServletResponse response) throws Exception{
		int cfcount = 0;
		int count = 0;
		for (int i = 0; i < mids.length; i++) {
			param.setMid(mids[i]);
			param.setUnitPrice(unitPrices[i]);
			SaleMaterielExpenseAccount saleMaterielExpenseAccount = new SaleMaterielExpenseAccount();
			saleMaterielExpenseAccount.setKid(param.getKid());
			saleMaterielExpenseAccount.setMid(param.getMid());
			List<SaleMaterielExpenseAccount> saleMaterielExpenseAccountList = saleMaterielExpenseAccountService.queryAllByMution(saleMaterielExpenseAccount);
			if(saleMaterielExpenseAccountList.size() > 0){
				cfcount++;
			}else{
				Long id = saleMaterielExpenseAccountService.insert(param);
				if(null != id){
					count++;
				}
			}
			saleMaterielExpenseAccount = null;
			saleMaterielExpenseAccountList = null;
		}
		ResponseUtil.write(response,count);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(SaleMaterielExpenseAccount param,HttpServletResponse response) throws Exception{
		Integer count = saleMaterielExpenseAccountService.update(param);
		ResponseUtil.write(response,count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = saleMaterielExpenseAccountService.delete(ids);
		ResponseUtil.write(response,count);
	}
}
